package com.app.jzgdirector.common.service;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;

public class AppManager {
    private ActivityStack<Activity> activityStack;
    private static AppManager instance;

    public static AppManager getAppManager() {
        if (instance == null) {
            instance = new AppManager();
        }
        return instance;
    }

    public void addActivity(Activity activity) {
        if (activityStack == null) {
            activityStack = new ActivityStack<Activity>();
        }
        activityStack.push(activity);
//		for(int i=0;i<activityStack.size();i++){
//			XLog.d("activityTest", activityStack.getString(i).getClass().getName());
//		}
    }

    public Activity currentActivity() {
        if (activityStack == null || activityStack.size() <= 0) {
            return null;
        }
        return activityStack.lastElement();
    }

    public void removeActivity(Activity activity) {
        if (activityStack == null || activity == null) {
            return;
        }
        activityStack.remove(activity);
    }

    public void finishActivity(Class<?> cls) {
        int size = activityStack.size();
        for (int i = size - 1; i >= 0; i--) {
            Activity activity = activityStack.peek();
            if (activity != null
                    && activity.getClass().equals(cls)) {
                activity.finish();
                activityStack.remove(activity);
                break;
            }
        }

    }

    public void finishAllActivity() {
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (null != activityStack.get(i)) {
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }

    public void appExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr = (ActivityManager) context
                    .getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.killBackgroundProcesses(context.getPackageName());
            System.exit(0);
        } catch (Exception e) {

        }
    }

    public void finishToActivity(Class<?> cls) {
        if (activityStack != null) {
            Activity a = activityStack.lastElement();
            while (!a.getClass().equals(cls) && hasValidActivity()) {
                activityStack.remove(a);
                a.finish();
                a = activityStack.lastElement();
            }
        }
    }

    public boolean isTopActivity(Class clazz) {
        Activity currentActivity = currentActivity();
        if (currentActivity == null) {
            return false;
        }
        return currentActivity.getClass()
                .equals(clazz);
    }


    public Activity getExistActivity(Class<?> cls) {
        for (int i = 0; i < activityStack.size(); i++) {
            if (activityStack.get(i) != null && activityStack.get(i).getClass().equals(cls)) {
                return activityStack.get(i);
            }
        }
        return null;
    }

    public void finishOtherActivity(Class clazz) {
        if (activityStack != null) {
            Activity a = activityStack.lastElement();
            while (a != null && !a.getClass().equals(clazz) && hasValidActivity()) {
                activityStack.remove(a);
                a.finish();
                a = activityStack.lastElement();
            }
        }

    }

    public void finishOtherActivityContainsBottom(Class clazz) {
        Activity target = null;
        for (int i = 0, size = activityStack.size(); i < size; i++) {
            if (activityStack.empty()) {
                continue;
            }
            Activity activity = activityStack.pop();
            if (activity == null) {
                continue;
            }
            if (!activity.getClass().equals(clazz)) {
                activity.finish();
            } else {
                target = activity;
            }
        }
        if (target != null) {
            activityStack.push(target);
        }

    }

    protected boolean hasValidActivity() {
        int size = activityStack.size();
        int validSize = 0;
        for (int i = 0; i < size; i++) {
            Activity activity = activityStack.get(i);
            if (activity != null) {
                validSize++;
            }
            if (validSize > 1) {
                return true;
            }
        }
        return false;
    }
}
